<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/*
 * Functions for templates
 */

 /**
  * returns the translated string identified by $key
  * and optionally replace %s ocurrencies for each string
  * in $params array
  *
  * @param object $key
  * @param object $params [optional]
  * @return string
  */
function lang($key, $params = NULL){
	$app = & get_instance();
	$line = $app->lang->line($key);
	if(gettype($params)=='array'){
		foreach($params as $keyparam => $param){
			$line = str_replace('%'.$keyparam, $param, $line);
		}
	}

	if ($app->session->userdata('enable_i18n_manager')){
		return '<span class="eiplang" id="'.$key.'" lang="english">'.$line.'</span>';
	}else{
		return $line;
	}
}

 /**
  * load a partial template or array of templates
  * optionally with params
  *
  * @param object $templates
  * @param object $params [optional]
  * @return string
  */
function template($templates, $params=array()){
	$app = & get_instance();
	$return = '';
	if ( gettype($templates)=='array' ){
		foreach($templates as $template){
			$return .= $app->load->view($template, $params, TRUE);
		}
	}else{
		$return  = $app->load->view($templates, $params, TRUE);
	}
	return $return;
}

/**
 * writes as HTML comment CSS array or JS array for get_css_scripts_comments
 * @param object $stringid
 * @param object $array_of_urls
 * @return HTML comment string
 */
function set_css_scripts_comments($stringid, $array_of_urls){
	$return = '<!--'.$stringid.' ';
	foreach($array_of_urls as $s){
		$return .= "|".$s."|";
	}
	return $return." -->";
}

/**
 * get css_scripts comments as urls array
 * @param object $stringid
 * @param object $output
 * @return array
 */
function get_css_scripts_comments($stringid, $output){
	$r = array();
	$result = array();
	$expression = '<!--'.$stringid.'[^\<]+-->';
	preg_match_all($expression, $output, $r);
	foreach ($r[0] as $a){
		$result_a = str_replace('||','|',$a);
		$result_a = substr($result_a, 16, strlen($result_a) -20);
		$result = array_unique( array_merge( $result, explode("|", $result_a) ) );
	}
	return $result;
}
/**
 * alias for set_css_scripts_comments
 * @param object $scripts
 * @return string
 */
function head_scripts($scripts){
	return set_css_scripts_comments('headscripts', $scripts);
}
function head_styles($styles){
	return set_css_scripts_comments('headstyles', $styles);
}

/**
 * alias for get_css_scripts_comments
 * @param object $output
 * @return array
 */
function get_head_scripts($output){
	return get_css_scripts_comments('headscripts', $output);
}
function get_head_styles($output){
	return get_css_scripts_comments('headstyles', $output);
}

/**
 * prints iframe tag for iframe (ajax simulated) responses
 * @param object $iframe_name [optional]
 * @return string
 */
function iframe_response($iframe_name = 'iframe_response'){
	return '<iframe src="about:blank" id="'.$iframe_name.'" name="'.$iframe_name.'" style="width:0; height:0; border: 0px #fff solid"></iframe>';
}

function issetArray( $array=NULL ){
	$return = ( ($array ==NULL)? array() : $array );
	return $return;
}

/**
 * returns "odd" or "even"
 * @param int $key
 */
function odd_even($key = 0){
	return $key%2 == 1 ? 'odd' : 'even';
}

 /*
  * Guarda un mensaje en cookie
  */
 function setMessage($msg){
 	$app = & get_instance();

 	if ($app->session->userdata("msgs")){
 		$msgs = $app->session->userdata("msgs");
 	}else{
 		$msgs = array();
 	}

	array_push($msgs,$msg);

	$app->session->set_userdata("msgs",$msgs);
 }

 /*
  * Imprime un mensaje o todos con un estilo
  */
 function getMessage($all=TRUE, $class="ok", $elem="p"){

	$app = & get_instance();

 	if($app->session->userdata("msgs")){

 		$msgs = $app->session->userdata("msgs");

		if ($all==TRUE){
			$string = "";
			for($i=0;$i < count($msgs);$i++){
				$string .= "<$elem class='$class'>".$app->lang->line($msgs[$i])."</$elem>";
			}

			$app->session->unset_userdata("msgs");
			return $string;
		}else{
			$string = "<$elem class='$class'>".$app->lang->line(array_pop($msgs))."</$elem>";
			if (count($msgs)<=1){
				$app->session->unset_userdata("msgs");
			}else{
				$app->session->set_userdata("msgs",$msgs);
			}
			return $string;
		}
 	}else{

 		return "";
 	}
 }

 /* END of the file */